<% url_options = params_for(:action => :add_existing) -%>
<% if request.xhr? -%>
<%= form_remote_tag :url => url_options,
                    :after => "$('#{loading_indicator_id(:action => :add_existing, :id => params[:id])}').style.visibility = 'visible'; Form.disable('#{element_form_id(:action => :add_existing)}');",
                    :complete => "$('#{loading_indicator_id(:action => :add_existing, :id => params[:id])}').style.visibility = 'hidden'; Form.enable('#{element_form_id(:action => :add_existing)}');",
                    :failure => "ActiveScaffold.report_500_response('#{active_scaffold_id}')",
                    :html => { :href => url_for(url_options),
                    :id => element_form_id(:action => :add_existing),
                    :class => 'create' } %>
<% else -%>
<%= form_tag url_options,
             :id => element_form_id(:action => :add_existing),
             :class => 'create' %>
<% end -%>

  <h4><%= active_scaffold_config.nested.label -%></h4>

  <% if request.xhr? -%>
    <div id="<%= element_messages_id(:action => :add_existing) %>" class="messages-container"></div>
  <% else -%>
    <%= render :partial => 'form_messages' %>
  <% end -%>

  <label for="<%= "record_#{active_scaffold_config.model}" %>"><%= active_scaffold_config.model %></label>
  <% select_options = active_scaffold_config.model.find(:all).collect {|c| [c.to_label, c.id]} -%>
  <% unless select_options.empty? -%>
    <%= select_tag 'associated_id', options_for_select(select_options) %>
  <% end -%>

  <p class="form-footer">
    <%= submit_tag as_('Create'), :class => "submit" %>
    <%= link_to as_('Cancel'), params_for(:action => 'list'), :class => 'cancel' %>
    <%= loading_indicator_tag(:action => :add_existing, :id => params[:id]) %>
  </p>

</form>
<script type="text/javascript">
Form.focusFirstElement('<%= element_form_id(:action => :add_existing) -%>');
</script>